package com.example.admin.mycustomview.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by admin on 2016/6/30.
 */
public class LayerViewThree extends View {

    // 刻度尺高度
    private static final int DIVIDING_RULE_HEIGHT = 70;
    // 距离左右间
    private static final int DIVIDING_RULE_MARGIN_LEFT_RIGHT = 10;
    // 第一条线距离边框距离
    private static final int FIRST_LINE_MARGIN = 5;
    // 打算绘制的厘米数
    private static final int DEFAULT_COUNT = 10;
    // 竖线宽度
    private static final float DEFAULT_LINE_WIDTH = (float) 2.0;
    // 绘制的厘米的最小间隔
    private int Default_space;

    private int mDividRuleHeight;
    private int mHalfRuleHeight;
    private int mDividRuleLeftMargin;
    private int mFirstLineMargin;
    private Context context;
    private Rect mOutRect;
    private Paint mPaint, mLpaint;// 画笔对象
    private int mViewWidth, mViewHeight;// 控件宽高

    private int MaxLineTop;
    private int MiddleLineTop;
    private int MinLineTop;

    public LayerViewThree(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        // 实例化画笔对象并设置其标识值
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        //竖线画笔
        mLpaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        //设置画笔为空心
        mPaint.setStyle(Paint.Style.STROKE);
        //设置画笔的线宽
        mPaint.setStrokeWidth((float)DEFAULT_LINE_WIDTH);
        mPaint.setStrokeWidth((float)DEFAULT_LINE_WIDTH);
        initData();
    }

    private void initData() {
        mDividRuleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                DIVIDING_RULE_HEIGHT, context.getResources().getDisplayMetrics());
        mHalfRuleHeight = mDividRuleHeight / 2;
        mDividRuleLeftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                DIVIDING_RULE_MARGIN_LEFT_RIGHT, context.getResources().getDisplayMetrics());
        mFirstLineMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                FIRST_LINE_MARGIN, context.getResources().getDisplayMetrics());

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        /*
         * 获取控件宽高
         */
        mViewWidth = w;
        mViewHeight = h;

        Default_space = (int) ((mViewWidth - (mDividRuleLeftMargin * 2)) / 100);

        //最长的线的坐标
        MaxLineTop = (mViewHeight - (mDividRuleLeftMargin * 2)) / 2 + mDividRuleLeftMargin;
        MiddleLineTop = ((mViewHeight - (mDividRuleLeftMargin * 2)) * 2) / 3 + mDividRuleLeftMargin;
        MinLineTop = ((mViewHeight - (mDividRuleLeftMargin * 2)) * 3) / 4 + mDividRuleLeftMargin;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制外框
        drawOuter(canvas);
        // 绘制刻度线
        drawLines(canvas);
        // 绘制数字
        drawNumbers(canvas);
    }


    private void drawOuter(Canvas canvas) {
        mOutRect = new Rect(mDividRuleLeftMargin, mDividRuleLeftMargin, mViewWidth - mDividRuleLeftMargin, mViewHeight - mDividRuleLeftMargin);
        canvas.drawRect(mOutRect, mPaint);
    }

    private void drawLines(Canvas canvas) {
        canvas.save();
        canvas.translate(mDividRuleLeftMargin + mFirstLineMargin, 0);
        int top = MaxLineTop;
        for (int i = 0; i <= DEFAULT_COUNT * 10; i++) {
            if (i % 10 == 0) {
                top = MaxLineTop;
            } else if (i % 5 == 0) {
                top = MiddleLineTop;
            } else {
                top = MinLineTop;
            }
            canvas.drawLine(0, mViewHeight - mDividRuleLeftMargin, 0, top, mLpaint);
            canvas.translate(Default_space, 0);

        }
        canvas.restore();
    }

    private void drawNumbers(Canvas canvas) {
        canvas.save();
        canvas.translate(mDividRuleLeftMargin + mFirstLineMargin, 0);
        int top = MaxLineTop;
        for (int i = 0; i <= DEFAULT_COUNT * 10; i++) {
            if (i % 10 == 0) {
                top = MaxLineTop;
            }
            canvas.drawText("" + i, -5, top-2, mLpaint);
            canvas.translate(Default_space*10, 0);
        }
        canvas.restore();
    }

}
